package CI.aufgabe2.cip.mynodes;

import CI.aufgabe2.cip.SymbolTable;
import CI.aufgabe2.cip.base.AbstractDescr;
import CI.aufgabe2.cip.descriptor.ArrayDescr;
import CI.aufgabe2.cip.descriptor.RecordDescr;

public class MyRecordSelectorNode extends MyAbstractNode {
		private static final long serialVersionUID = 1L;
		
		public final MyAbstractNode subject;
		public final MyAbstractNode selector;
		
		public MyRecordSelectorNode(MyAbstractNode subject, MyAbstractNode selector){
			this.subject=subject;
			this.selector=selector;
		}

		@Override
		public void print() {
			trace("RecordSelectorNode");
			if(subject != null){
				subject.print();
			}
			if(selector != null){
				selector.print();
			}
			unindent();
		}
		
		
		public AbstractDescr compile(SymbolTable sym) {
			RecordDescr desc = null;
			if(subject instanceof MyIdNode){
				desc = (RecordDescr)sym.getDescr(((MyIdNode) subject).strval);
				subject.compile(sym);
				int i = desc.addressOf(((MyIdNode)selector).strval);
				write("PUSHI, "+i);
	    		write("ADD");
	    		return desc.getDesc(((MyIdNode)selector).strval);
//	    		return desc;
			}else{
				AbstractDescr abValue = subject.compile(sym);
				AbstractDescr reval = null;
				int size =0;
				
				if(subject instanceof MyRecordSelectorNode){
					size = ((RecordDescr)abValue).addressOf(((MyIdNode)selector).strval);
					write("PUSHI, "+ size);
		    		write("ADD");
					return ((RecordDescr)abValue).getDesc(((MyIdNode)selector).strval);
				}
//				else if(subject instanceof MyArraySelectorNode){
//					selector.compile(sym);
//					if(selector instanceof MyIdNode)
//			    	 write("CONT, 1");
//					size = ((ArrayDescr)abValue).GetBasetype().getSize();
//				}
				
	    		
	    		return abValue;
			}
//				if(subject instanceof MyRecordSelectorNode){
//				desc = (RecordDescr)subject.compile(sym);
//				int i = desc.addressOf(((MyIdNode)selector).strval);
//				write("PUSHI, "+i);
//	    		write("ADD");
//	    		
//				}
////				if(subject instanceof MyArraySelectorNode){
//					ArrayDescr value = (ArrayDescr) subject.compile(sym);
//					String v = ((MyIdNode)((MyArraySelectorNode) subject).subject).strval;
//					SymbolTable sys = ((RecordDescr)((ArrayDescr)sym.getDescr(v)).GetBasetype()).getSymbolTable();
////					selector.compile(sys);
////					if(selector instanceof MyIdNode)
////				    	 write("CONT, 1");
//					write("PUSHI, "+ value.GetBasetype().getSize());
//		    		write("ADD");
////		    		return value.GetBasetype();
//		    		return value;
//				}
			
//			return desc.getDesc(((MyIdNode)selector).strval);
//			return desc;
			
		}

}
